package com.example.memorycardgame.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.memorycardgame.R;
import com.example.memorycardgame.model.UserData;
import com.example.memorycardgame.utils.DataStorageUtil;

public class ProfileFragment extends Fragment {
    private UserData userData;
    private TextView tvName, tvGender, tvPhone, tvHigh2, tvHigh3;
    private EditText etName, etGender, etPhone;
    private Button btnEdit, btnSave;
    private View layoutDisplay, layoutEdit;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        initViews(view);
        loadUserData();
        updateInfoDisplay();
        bindEvents();
        return view;
    }

    private void initViews(View view) {
        tvName = view.findViewById(R.id.tv_name);
        tvGender = view.findViewById(R.id.tv_gender);
        tvPhone = view.findViewById(R.id.tv_phone);
        tvHigh2 = view.findViewById(R.id.tv_high_score_2card);
        tvHigh3 = view.findViewById(R.id.tv_high_score_3card);
        etName = view.findViewById(R.id.et_name);
        etGender = view.findViewById(R.id.et_gender);
        etPhone = view.findViewById(R.id.et_phone);
        btnEdit = view.findViewById(R.id.btn_edit);
        btnSave = view.findViewById(R.id.btn_save);
        layoutDisplay = view.findViewById(R.id.layout_info_display);
        layoutEdit = view.findViewById(R.id.layout_info_edit);
    }

    // 加载序列化的用户数据
    private void loadUserData() {
        userData = DataStorageUtil.loadUserData(requireContext());
    }

    // 更新信息显示
    private void updateInfoDisplay() {
        tvName.setText("姓名：" + userData.getUserName());
        tvGender.setText("性别：" + userData.getUserGender());
        tvPhone.setText("电话：" + userData.getUserPhone());
        tvHigh2.setText("2张牌最高分：" + userData.getHighScore2Card());
        tvHigh3.setText("3张牌最高分：" + userData.getHighScore3Card());
    }

    private void bindEvents() {
        // 编辑按钮：切换到修改界面
        btnEdit.setOnClickListener(v -> {
            layoutDisplay.setVisibility(View.GONE);
            layoutEdit.setVisibility(View.VISIBLE);
            // 填充当前信息到输入框
            etName.setText(userData.getUserName());
            etGender.setText(userData.getUserGender());
            etPhone.setText(userData.getUserPhone());
        });

        // 保存按钮：更新信息并序列化存储
        btnSave.setOnClickListener(v -> {
            String newName = etName.getText().toString().trim();
            String newGender = etGender.getText().toString().trim();
            String newPhone = etPhone.getText().toString().trim();

            if (newName.isEmpty()) {
                etName.setError("姓名不能为空");
                return;
            }

            // 更新数据
            userData.setUserName(newName);
            userData.setUserGender(newGender);
            userData.setUserPhone(newPhone);
            // 保存到文件
            DataStorageUtil.saveUserData(requireContext(), userData);
            // 切换回显示界面
            layoutDisplay.setVisibility(View.VISIBLE);
            layoutEdit.setVisibility(View.GONE);
            updateInfoDisplay();
        });
    }

    // 提供外部更新最高分的方法
    public void updateHighScore(boolean is2Card, int newScore) {
        if (is2Card) {
            if (newScore > userData.getHighScore2Card()) {
                userData.setHighScore2Card(newScore);
            }
        } else {
            if (newScore > userData.getHighScore3Card()) {
                userData.setHighScore3Card(newScore);
            }
        }
        DataStorageUtil.saveUserData(requireContext(), userData);
        updateInfoDisplay();
    }
}